package com.share.sentinel.rest;

import com.share.sentinel.response.ResponseData;
import com.share.sentinel.service.SentinelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author caifan
 * @created 2021/4/28
 * description:
 */
@RestController
@RequestMapping("/sentinel")
public class SentinelRest {

    @Autowired
    private SentinelService sentinelService;

    @GetMapping("/test_flow_rule")
    public ResponseData testFlowRule() {
        return new ResponseData("0", "success");
    }

    @GetMapping("/degrade_flow_rule")
    public ResponseData createOrderDegrade() {
        try {
            sentinelService.createOrder();
        } catch (IllegalStateException e) {
            //当 createOrder 业务处理过程中产生错误时会抛出IllegalStateException
            //IllegalStateException 是 JAVA 内置状态异常，在项目开发时可以更换为自己项目的自定义异常
            //出现错误后将异常封装为响应对象后JSON输出
            return new ResponseData(e.getClass().getSimpleName(),e.getMessage());
        }
        return new ResponseData("0", "order created");
    }

    @GetMapping("/param")
    public ResponseData paramFlow(@RequestParam(required = false) String param1, @RequestParam(required = false) String param2) {
        sentinelService.queryOrder(param1, param2);
        return new ResponseData("0", "hot param");
    }
}
